<?php
// 角色模块
class PrivilegeAction extends CommonAction {
	
	function _filter(&$map){
		$map['name'] = array('like',"%".$_POST['name']."%");
	}
	//自定义单一模块无须认证的方法
	public function _noAuth()
	{
		//默认方法index,getlist是模块认证后无须认证的
		if(in_array(ACTION_NAME,array('index','getNodeData','getRight','setRight'))) return true;
		else return false;
	}
     /**
     +----------------------------------------------------------
     * 增加组操作权限
     *
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
	public function index()
	{
		$groupId = isset($_GET['groupId'])?$_GET['groupId']:'';
		
		$this->assign("groupId",$groupId);
		//读取系统组列表
		$group   =  D('Role');
	    $list=  $group->field('id,name as text')->order("pid asc,id asc")->select();
	/*	foreach ($list as $vo){
			$groupList[$vo['id']]	=	$vo['name'];
		}*/
		$this->assign("roleTreeData",json_encode($list));
		$this->display();
	}
	//读取当前权限数据
	public function getRight()
	{
		//读取系统组列表
		$role   =  D('Role');
		$groupId	=	$_POST['groupId'];
		$list       =  $role->getAcceessList($groupId);
		echo json_encode($list);
	}


	public function setRight()
	{
		$groupId	=	$_POST['roleId'];
		$roleName   =   $_POST['roleName'];
		$accessList =   $_POST['accessList'];
		$group    =   D("Role");
		$group->delAcceessList($groupId);
		$result = $group->setAcceessList($groupId,$accessList);
		
		if($result===false) {
			echo '角色组['.$roleName.']授权失败！';
		}else {
			echo '角色组['.$roleName.']授权成功！';
		}
		
	}
	
	public function getNodeData(){
    	
    	$model = D('Node');
    	$map = $this->_searchRule();
    	$rows = $model->where($map)->order("pid asc,id asc")->select();
    	$result = array();
    	$I = array();
    	//Log::write('调试的SQL语句：' . $model->getLastSql(), Log::SQL);
        if(!empty($rows)){
    		$parentid=$rows[0]['pid'];
    	}
    	else {
    		$parentid=0;
    	}
    	foreach($rows as $val) {
    		if($val['pid'] == $parentid) {
    		//if(!isset($val['_flag'])) {
    			$i = count($result);
    			$result[$i] = $val;
    			$I[$val['id']] = & $result[$i];
    		} else {
    			if(isset($I[$val['pid']]['children'])){
    				$i = count($I[$val['pid']]['children']);
    			}
    			else{
    				$i=0;
    			}
    			$I[$val['pid']]['children'][$i] = $val;
    			$I[$val['id']] = & $I[$val['pid']]['children'][$i];
    		}
    	}
    	//exit(print_r($result));
    	echo json_encode(array('Rows'=>$result));
    }

}
?>